var DYNAMIC_REGS = [];
var DYNAMIC_PATH = [];
/**
 * 带版本路径判断正在
 * @type {RegExp}
 */
const IS_VER_END_RE = /(([\.\d]+)?[\.]){1}\d+[\/]?$/;
var dynamicPath = {
    "iport": [{
        "path": "/iport/1.0.0"
        , "reg": "31612.iport.com.cn"
        , "defColor": "orange"
    }]
};

function each(obj, cb) { 
    for(var key in obj){
        cb(obj[key], key);
    }
}

// 根据配置项生成业务需要的数据
// 根据配置项生成业务需要的数据
each(dynamicPath, (val, key) => {
    if (Array.isArray(val)) {
        each(val, (_val) => buildPathMapper(_val, key))
    }else {
        buildPathMapper(val, key)
    } 
});

console.log('DYNAMIC_PATH:');
console.log(DYNAMIC_PATH);
console.log(DYNAMIC_REGS);

common = null;
dynamicPath = null;

function buildPathMapper(val, key) {
    if (val.reg && val.path) {
        DYNAMIC_REGS.push(
            new RegExp(val.reg)
        );
        val.type = key;
        let ver = val.path.match(IS_VER_END_RE)[0];
        if (ver) {
            val.ver = ver;
        }
        DYNAMIC_PATH.push(val);
    }
}

function check(host, pathname) {
    if (host) {
        let re = null;
        let index = 0;
        while (index < DYNAMIC_REGS.length) {
            let reg = DYNAMIC_REGS[index];
            if (reg.test(host)) {
                re = DYNAMIC_PATH[index];
                break;
            }
            ++index;
        }
        if (re && pathname) {
            if (!IS_VER_END_RE.test(pathname)) {
                re = null;
            }
        }
        return re;
    }
    return null;
}